package com.javaleague.sesionUsuario;

import java.io.IOException;
import com.javaleague.Constantes;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import com.javaleague.persistencia.DAOLogin;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author TaixMiguel
 */

public class Logeo extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException {

        String mail =  request.getParameter("mail");
    	String pass = request.getParameter("pass");
                    
        try {
            // Compruebo si el usuario es el correcto
            Usuario user = DAOLogin.readUserLogin(mail, pass, request.getRemoteAddr());
            if ( user != null ) {
                HttpSession session = request.getSession(true);
                session.setAttribute( "usuario", user);
                response.sendError(200);
            } else
                response.sendError(601);
            
        } catch (IOException ex) {
            com.javaleague.informacion.ErrorGeneral.guardarLog(Object.class.getName(), Constantes.Informacion.ErrorMensajes.IOException, ex);
        }               
    }

    
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException {
        processRequest(request, response);
    }
}